import { defineStore } from 'pinia'
import { 
  getArticleComments, 
  addComment
} from '@/api/comment'
import type { Comment } from '@/models/comment.model'

export const useCommentStore = defineStore('comment', {
  state: () => ({
    comments: [] as Comment[]
  }),
  actions: {
    // 获取文章评论
    async fetchArticleComments(articleId: string) {
      try {
        const response = await getArticleComments(articleId)
        this.comments = response.data || []
        return response
      } catch (error) {
        throw error
      }
    },
    
    // 添加评论
    async addComment(comment: Comment) {
      try {
        const response = await addComment(comment)
        this.comments.unshift(response)
        return response
      } catch (error) {
        throw error
      }
    }
  }
})